using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
using WFTools;

public class TableDataMgr:SingleMono<TableDataMgr> 
{ 
	public Dictionary<string,CampaignStageSettings> CampaignStageSettingsTB => CampaignStageSettingstb;
 	Dictionary<string,CampaignStageSettings> CampaignStageSettingstb;
 	public Dictionary<string,Formation> FormationTB => Formationtb;
 	Dictionary<string,Formation> Formationtb;
 	public Dictionary<string,MonsterSetting> MonsterSettingTB => MonsterSettingtb;
 	Dictionary<string,MonsterSetting> MonsterSettingtb;
 	public Dictionary<string,WaveTable> WaveTableTB => WaveTabletb;
 	Dictionary<string,WaveTable> WaveTabletb;
 
	public void Init()
	{
		Dictionary<string, SheetData> sheetDatas = BinarySerializationHelper.DeserializeFromBinary <Dictionary<string, SheetData>> (File.ReadAllBytes(Path.Combine(XMLConfig.dataPath, "TBData")));
		CampaignStageSettingstb = new Dictionary<string,CampaignStageSettings>();
		for (int i = 4; i < sheetDatas["CampaignStageSettings"].datas.Count; i++)
		{
			if (!XMLConfig.CheckStartsWith(sheetDatas["CampaignStageSettings"].datas[i][0], XMLConfig.jumpTag))
			CampaignStageSettingstb.Add(sheetDatas["CampaignStageSettings"].datas[i][0],new CampaignStageSettings(sheetDatas["CampaignStageSettings"].datas[i])) ;
		}
		Formationtb = new Dictionary<string,Formation>();
		for (int i = 4; i < sheetDatas["Formation"].datas.Count; i++)
		{
			if (!XMLConfig.CheckStartsWith(sheetDatas["Formation"].datas[i][0], XMLConfig.jumpTag))
			Formationtb.Add(sheetDatas["Formation"].datas[i][0],new Formation(sheetDatas["Formation"].datas[i])) ;
		}
		MonsterSettingtb = new Dictionary<string,MonsterSetting>();
		for (int i = 4; i < sheetDatas["MonsterSetting"].datas.Count; i++)
		{
			if (!XMLConfig.CheckStartsWith(sheetDatas["MonsterSetting"].datas[i][0], XMLConfig.jumpTag))
			MonsterSettingtb.Add(sheetDatas["MonsterSetting"].datas[i][0],new MonsterSetting(sheetDatas["MonsterSetting"].datas[i])) ;
		}
		WaveTabletb = new Dictionary<string,WaveTable>();
		for (int i = 4; i < sheetDatas["WaveTable"].datas.Count; i++)
		{
			if (!XMLConfig.CheckStartsWith(sheetDatas["WaveTable"].datas[i][0], XMLConfig.jumpTag))
			WaveTabletb.Add(sheetDatas["WaveTable"].datas[i][0],new WaveTable(sheetDatas["WaveTable"].datas[i])) ;
		}
	}

    public Dictionary<T, U> ConvertMap<T, U>(string str, char kvspl, char arryspl, Func<string, T> convertKey, Func<string, U> convertValue)
    {
		if (str == null || str.Length == 0 || str == "-" || str == "0") return null;
        Dictionary<T, U> dic = new Dictionary<T, U>();
        string[] items = str.Split(arryspl);
        string[] kvs;
        for (int i = 0; i < items.Length; i++)
        {
            kvs = items[i].Split(kvspl);
            dic.Add(convertKey(kvs[0]), convertValue(kvs[1]));
        }
        return dic;
    }
    public List<T> ConvertTypeList<T>(string data,char itemSpl,char fieldSpl, Func<List<string>,T> converter) 
    { 
		if (string.IsNullOrEmpty(data)  || data.Length == 0 || data == "-" || data == "0") return null;
        List<T> list = new List<T>();
        data = data.Trim('\n');
        string[] items = data.Split(itemSpl);
        for (int i = 0; i < items.Length; i++) {
            string[] fields = items[i].TrimEnd('\n').Split(fieldSpl);
            list.Add( converter.Invoke(new List<string>(fields)));
        }
        return list;
    }
} 
